#!/usr/bin/perl

# Do simulation results file globbing
$outputdir = "/home/rumen/simulation1";
$query = "K=0_P=0.25_MU=1e-06_NU=1e-05";

# Get all sweeps that match the query
opendir(DIR, $outputdir);
@files = grep { /$query/ } readdir(DIR);
closedir(DIR);

foreach $f (@files) {
    print STDERR "Processing sweep $f...\n";
    $dir = $outputdir."/".$f;
    print STDERR "$dir\n";
    opendir(DIR, $dir);    
    @clonesfiles = grep { /clones$/ } readdir(DIR);
    rewinddir(DIR);
    @patternsfiles = grep { /patterns$/ } readdir(DIR);
    closedir(DIR);

    # go to the folder of that sweep
    chdir($dir);
    foreach $cf (@clonesfiles) {
	foreach $pf (@patternsfiles){
	    @r1 = split (/\./, $cf);
	    @r2 = split (/\./, $pf);
	    # Only when we have output for clones and patterns of a particular replicate
	    if($r1[0] eq $r2[0]){
		print STDERR "Processing $cf and $pf replicate...\n";

# Remove duplicates from the .clones log
		my %h=();
		open(in, $cf);
		open(out, ">$cf.out");
		while(<in>){
		    chomp;
		    @r = split(/\,/);
		    if(!defined($h{$r[0]})){
			print out $_."\n";
		    }
		    $h{$r[0]}=1;
		}
		close out;
		close in;

		%h = ();
# Re-read the file and output the line for a clone only once
		open(in, "$pf");
		open(out, ">$pf.out");
		while(<in>){
		    chomp;
		    @r = split(/\,/);
		    if(!defined($h{$r[0]})){
			print out $_."\n";
		    }
		    $h{$r[0]}=1;
		}
		close out;
		close in;		

		@args = "mv $cf.out $cf";
		system(@args);
		@args = "mv $pf.out $pf";
		system(@args);
	    }
	}
    }
}
